package com.situ.object.controll;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.situ.object.common.Page;
import com.situ.object.common.ResponseBean;
import com.situ.object.entity.Course;
import com.situ.object.entity.Student;
import com.situ.object.service.CourseService;
import com.situ.object.service.StudentService;
import com.situ.object.service.impl.CourseServiceImpl;
import com.situ.object.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/course")
public class CourseServlet extends HttpServlet {
    //合成
    private CourseService courseService = new CourseServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置req编码格式和resp内容类型
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        //二、获取参数
        //根据jsp页面的ajax传输地址中的method的属性值进行分支
        String method = req.getParameter("method");
        if ("getForPage".equals(method)){
            getForPage(req,resp);
        }else if ("add".equals(method)){
            add(req,resp);
        }else if ("edit".equals(method)){
            edit(req,resp);
        }else if ("delete".equals(method)){
            delete(req,resp);
        }
        else {
            System.out.println("----------");
        }

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除，根据id来删除用户
        //获取参数
        Integer goods_id= Integer.valueOf(req.getParameter("id"));

        try{

            courseService.delete(goods_id);

            //返回s
            //创建jackson的核心对象
            ObjectMapper objectMapper=new ObjectMapper();
            String jsonStr=objectMapper.writeValueAsString(ResponseBean.ok());
            resp.getWriter().append(jsonStr);
        }catch (Exception e){
            ObjectMapper objectMapper=new ObjectMapper();
            String jsonStr=objectMapper.writeValueAsString(ResponseBean.failed(e.getMessage()==null?"删除失败":e.getMessage()));
            resp.getWriter().append(jsonStr);
        }
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数  Integer.valueOf 转为int类型
        Integer course_id = Integer.valueOf(req.getParameter("course_id"));
        String course_name = req.getParameter("course_name");
        Integer student_id = Integer.valueOf(req.getParameter("student_id"));
        Integer course_credit = Integer.valueOf(req.getParameter("course_credit"));
        Integer status = Integer.valueOf(req.getParameter("status"));
        String type = req.getParameter("type");

        //调用service 首先合成
        Course course = new Course();
        course.setCourseId(course_id);
        course.setCourseName(course_name);
        course.setStudentId(student_id);
        course.setCourseCredit(course_credit);
        course.setStatus(status);
        course.setType(type);


        try {

            courseService.edit(course);

            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.ok());
            resp.getWriter().append(jsonStr);
        } catch (Exception e) {
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.failed(e.getMessage()==null?"编辑失败":e.getMessage()));
            resp.getWriter().append(jsonStr);
        }
    }

    private void add(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
        //获取参数


        String course_name = req.getParameter("course_name");
        String student_id = req.getParameter("student_id");
        Integer course_credit = Integer.valueOf(req.getParameter("course_credit"));
//        Integer status = Integer.valueOf(req.getParameter("status"));
        String type = req.getParameter("type");
        //将参数set给用户的实现类对象
        Course course = new Course();
        course.setCourseName(course_name);
        course.setStudentId(Integer.valueOf(student_id));
        course.setCourseCredit(course_credit);
//        course.setStatus(status);
        course.setType(type);

        //调用service 首先合成类
        int i = courseService.add(course);
        System.out.println(course_credit);
        if (i > 0){
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.ok());
            resp.getWriter().append(jsonStr);
        }else {
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.failed("添加失败"));
            resp.getWriter().append(jsonStr);
        }
    }


    private void getForPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数 page 当前页面 limit 每页条数
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));

        //多条件查询 学生姓名和教师id
        Course course = new Course();
        course.setCourseName(req.getParameter("course_name"));
        //处理teacher_id为空的情况
        try{
            course.setStudentId(Integer.valueOf(req.getParameter("student_id")));
        }catch (Exception e){
            System.out.println("teacher_id为空");
        }
//        System.out.println(student);
        //调用业务处理
        System.out.println("courseServlet:"+course);
        Page pageInfo = courseService.getForPage(page,limit,course);
        System.out.println("course_pageInfo:"+pageInfo);
        //返回
        //创建jackson的核心对象
        ObjectMapper objectMapper=new ObjectMapper();
        String jsonStr=objectMapper.writeValueAsString(ResponseBean.ok(0,pageInfo));
        resp.getWriter().append(jsonStr);
    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}


